package com.esyoga.yoyoga.service.impl;

import com.esyoga.yoyoga.mapper.*;
import com.esyoga.yoyoga.pojo.*;
import com.esyoga.yoyoga.pojo.DTO.GymAgreementDTO;
import com.esyoga.yoyoga.service.GymService;
import com.esyoga.yoyoga.service.PlatformService;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@Service("platformService")
@Transactional
public class PlatformServiceImpl implements PlatformService {

    @Resource
    private TGymAgreementExample tGymAgreementExample;
    @Resource
    private TGymAgreementMapper tGymAgreementMapper;
    @Resource
    private TTrainerAgreementExample tTrainerAgreementExample;
    @Resource
    private TTrainerAgreementMapper tTrainerAgreementMapper;
    @Resource
    private GymService gymService;
    @Resource
    private TGymMapper tGymMapper;
    @Resource
    private TGymExample tGymExample;
    @Resource
    private TTrainerExample tTrainerExample;
    @Resource
    private TTrainerMapper tTrainerMapper;
    @Resource
    private TInformMapper tInformMapper;
    @Resource
    private TStudentMapper tStudentMapper;
    @Resource
    private TStudentExample tStudentExample;
    @Resource
    private TOrderMapper tOrderMapper;
    @Resource
    private TOrderExample tOrderExample;
    @Resource
    private TPlatformMapper tPlatformMapper;
    @Resource
    private TEvaluationExample tEvaluationExample;
    @Resource
    private TEvaluationMapper tEvaluationMapper;
    @Resource
    private TContractMapper tContractMapper;

    /*与场馆解约*/
    @Override
    public void rescissionWithGym(int gid) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime=simpleDateFormat.format(new Date());
        /*在场馆签约表中找到这个签约状态改为解约，更新更新时间*/
        tGymAgreementExample.clear();
        TGymAgreementExample.Criteria criteriaGym=tGymAgreementExample.createCriteria();
        criteriaGym.andGaGIdEqualTo(gid);
        TGymAgreement tGymAgreement=new TGymAgreement();
        tGymAgreement.setGaState(3);
        tGymAgreement.setGaUpdatetime(nowTime);
        tGymAgreementMapper.updateByExampleSelective(tGymAgreement,tGymAgreementExample);
        /*更新场馆表中的签约状态（改为1，未签约）*/
        TGym tGym=new TGym();
        tGym.setgId(gid);
        tGym.setgState(1);
        tGymMapper.updateByPrimaryKeySelective(tGym);
        /*将所有与此场馆签约的教练都解约，向信息表中加入解约通知*/
        //找到所有教练id
        List<TTrainer> trainers = gymService.findTrainersByGymid(gid);
        List<Integer> tids=new ArrayList<>();
        for (TTrainer t:trainers) {
            tids.add(t.gettId());
        }
        //遍历向信息表中加入解约通知
        if (tids.size()!=0&&tids!=null){
            for (Integer tid:tids) {
                gymService.rescissionWithTrainer(gid, tid);
            }
        }
        /*与所有签约教练解约*/
        tTrainerAgreementExample.clear();
        TTrainerAgreementExample.Criteria criteriaTrainer=tTrainerAgreementExample.createCriteria();
        criteriaTrainer.andTaGIdEqualTo(gid).andTaStateEqualTo(4);
        TTrainerAgreement tTrainerAgreement=new TTrainerAgreement();
        tTrainerAgreement.setTaState(5);
        tTrainerAgreement.setTaUpdatetime(nowTime);
        tTrainerAgreementMapper.updateByExampleSelective(tTrainerAgreement,tTrainerAgreementExample);
    }

    /*添加场馆*/
    @Override
    public void addGym(TGym tGym){
        tGymMapper.insertSelective(tGym);
    }

    /*查询所有未审核的场馆签约请求*/
    @Override
    public List<GymAgreementDTO> findGymAgreementsByState() {
        //找到所有未审核签约请求
        tGymAgreementExample.clear();
        TGymAgreementExample.Criteria criteria=tGymAgreementExample.createCriteria();
        criteria.andGaStateEqualTo(0);
        List<TGymAgreement> tGymAgreements=tGymAgreementMapper.selectByExample(tGymAgreementExample);
        List<GymAgreementDTO> gymAgreementDTOS=new ArrayList<>();
        //把场馆名字加进来
        for (TGymAgreement ga:tGymAgreements) {
            String gName=tGymMapper.selectByPrimaryKey(ga.getGaGId()).getgName();
            GymAgreementDTO gymAgreementDTO=new GymAgreementDTO(gName,ga);
            gymAgreementDTOS.add(gymAgreementDTO);
        }
        return gymAgreementDTOS;
    }


    /*平台是否同意与场馆签约,flag=1则同意签约，flag=0则拒绝签约*/
    @Override
    public String GymContract(int gaid,int flag) {
        int gid=tGymAgreementMapper.selectByPrimaryKey(gaid).getGaGId();
        BigDecimal joinBanlance=tGymAgreementMapper.selectByPrimaryKey(gaid).getGaJoinbanlance();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime=simpleDateFormat.format(new Date());
        if(flag==0){
            /*把场馆的钱退回去*/
            BigDecimal gymBalance=gymService.findBalanceByGid(gid);
            tGymExample.clear();
            TGymExample.Criteria criteria=tGymExample.createCriteria();
            criteria.andGIdEqualTo(gid);
            TGym tGym=new TGym();
            tGym.setgBanlance(gymBalance.add(joinBanlance));
            tGymMapper.updateByExampleSelective(tGym,tGymExample);
            /*把签约表中的状态改为被拒绝（1）*/
            TGymAgreement tGymAgreement=new TGymAgreement();
            tGymAgreement.setGaId(gaid);
            tGymAgreement.setGaState(1);
            tGymAgreement.setGaUpdatetime(nowTime);
            tGymAgreementMapper.updateByPrimaryKeySelective(tGymAgreement);
            return "拒绝了与该场馆的签约请求，平台入住费已退还该场馆";
        }
        /*钱划给平台*/
        TPlatform tPlatform=new TPlatform();
        tPlatform.setpId(1);
        tPlatform.setpBanlance(countBalance().add(joinBanlance));
        tPlatformMapper.updateByPrimaryKeySelective(tPlatform);
        /*改签约状态为签约成功（2）*/
        TGymAgreement tGymAgreement=new TGymAgreement();
        tGymAgreement.setGaId(gaid);
        tGymAgreement.setGaState(2);
        tGymAgreement.setGaSigntime(nowTime);
        tGymAgreement.setGaUpdatetime(nowTime);
        tGymAgreementMapper.updateByPrimaryKeySelective(tGymAgreement);
        return "签约成功";
    }

    /*教练审核*/
    @Override
    public void trainerAudit(int tid, int flag) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime=simpleDateFormat.format(new Date());
        TInform tInform=new TInform();
        tInform.setiUserid(tid);
        tInform.setiUsertype(1);
        tInform.setiTime(nowTime);
        /*flag=1表示审核通过，改教练状态，发审核通过的信息*/
        if(flag==1){
            TTrainer tTrainer=new TTrainer();
            tTrainer.settId(tid);
            tTrainer.settState(1);
            tTrainerMapper.updateByPrimaryKeySelective(tTrainer);
            tInform.setiText("您的教练资质审核通过，恭喜成为本平台认证的教练");
            tInformMapper.insertSelective(tInform);
        }else{
            /*flag=0表示审核未通过，发审核未通过的信息*/
            tInform.setiText("很遗憾，您的教练资质审核未通过");
            tInformMapper.insertSelective(tInform);
        }
    }

    /*教练禁用*/
    @Override
    public void trainerBan(int tid) {
        /*改教练状态为禁用*/
        TTrainer tTrainer=new TTrainer();
        tTrainer.settId(tid);
        tTrainer.settState(2);
        tTrainerMapper.updateByPrimaryKeySelective(tTrainer);
        /*添加教练禁用的消息*/
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime=simpleDateFormat.format(new Date());
        TInform tInform=new TInform();
        tInform.setiUserid(tid);
        tInform.setiUsertype(1);
        tInform.setiTime(nowTime);
        tInform.setiText("您的账户已被封禁");
        tInformMapper.insertSelective(tInform);
    }

    /*教练解封*/
    @Override
    public void trainerUnblocked(int tid) {
        /*改教练状态为未审核*/
        TTrainer tTrainer=new TTrainer();
        tTrainer.settId(tid);
        tTrainer.settState(0);
        tTrainerMapper.updateByPrimaryKeySelective(tTrainer);
        /*添加教练禁用的消息*/
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime=simpleDateFormat.format(new Date());
        TInform tInform=new TInform();
        tInform.setiUserid(tid);
        tInform.setiUsertype(1);
        tInform.setiTime(nowTime);
        tInform.setiText("您的账户已解封，正等待平台重新进行资质审核");
        tInformMapper.insertSelective(tInform);
    }

    @Override
    public List<TGym> findGyms() {
        List<TGym> tGyms=tGymMapper.selectByExample(null);
        return tGyms;
    }

    @Override
    public List<TGym> findGymsByStateZero(){
        tGymExample.clear();
        TGymExample.Criteria criteria=tGymExample.createCriteria();
        criteria.andGStateEqualTo(0);
        List<TGym> tGyms=tGymMapper.selectByExample(tGymExample);
        return tGyms;
    }

    /*展示所有教练（包括禁用）*/
    @Override
    public List<TTrainer> findTrainers() {
        List<TTrainer> tTrainers=tTrainerMapper.selectByExample(null);
        return tTrainers;
    }

    @Override
    public List<TTrainer> findTrainersByStateOne() {
        tTrainerExample.clear();
        TTrainerExample.Criteria criteria=tTrainerExample.createCriteria();
        criteria.andTStateEqualTo(1);
        return tTrainerMapper.selectByExample(tTrainerExample);
    }

    @Override
    public List<TTrainer> findTrainersByStateTwo() {
        tTrainerExample.clear();
        TTrainerExample.Criteria criteria=tTrainerExample.createCriteria();
        criteria.andTStateEqualTo(2);
        return tTrainerMapper.selectByExample(tTrainerExample);
    }

    @Override
    public List<TTrainer> findTrainersByStateZero() {
        tTrainerExample.clear();
        TTrainerExample.Criteria criteria=tTrainerExample.createCriteria();
        criteria.andTStateEqualTo(0);
        return tTrainerMapper.selectByExample(tTrainerExample);
    }

    /*展示所有用户*/
    @Override
    public List<TStudent> findStudents() {
        List<TStudent> students=tStudentMapper.selectByExample(null);
        return students;
    }

    /*查询所有订单*/
    @Override
    public List<TOrder> findOrders() {
        List<TOrder> orders=tOrderMapper.selectByExample(null);
        return orders;
    }

    /*根据订单号查评价*/
    @Override
    public TEvaluation findEvalution(int eoid) {
        tEvaluationExample.clear();
        TEvaluationExample.Criteria criteria=tEvaluationExample.createCriteria();
        criteria.andEOIdEqualTo(eoid).andEFlagEqualTo(0);
        List<TEvaluation> tEvaluations=tEvaluationMapper.selectByExample(tEvaluationExample);
        if (tEvaluations.size()==0){
            return null;
        }
        TEvaluation tEvaluation=tEvaluations.get(0);
        return tEvaluation;
    }

    /*查询所有合约*/
    @Override
    public List<TContract> findTContract() {
        return tContractMapper.selectByExample(null);
    }

    /*根据订单号查合约*/
    @Override
    public TContract findContractByOid(int oid){
        int cid=tOrderMapper.selectByPrimaryKey(oid).getoCId();
        return tContractMapper.selectByPrimaryKey(cid);
    }

    /*根据学员id查学员姓名*/
    @Override
    public String findSnameBySid(int sid) {
        return tStudentMapper.selectByPrimaryKey(sid).getsName();
    }

    /*根据教练id查教练姓名*/
    @Override
    public String findTnameByTid(int tid) {
        return tTrainerMapper.selectByPrimaryKey(tid).gettName();
    }

    /*根据场馆id查场馆名*/
    @Override
    public String findGnameByGid(int gid) {
        return tGymMapper.selectByPrimaryKey(gid).getgName();
    }

    /*查询余额*/
    @Override
    public BigDecimal countBalance() {
        BigDecimal balance=tPlatformMapper.selectByPrimaryKey(1).getpBanlance();
        return balance;
    }

}
